//
//  AssignedCustomersViewController.m
//  Balance
//
//  Created by Sophia Tsay on 6/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AssignedCustomersViewController.h"
#import "CustomerDetailTableViewController.h"
#import "TopDelinquentCustomersViewController.h"
#import "ColorUtility.h"

/*
#define delinquentCustomers [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys: @"Apple Inc", @"Customer",[NSNumber numberWithInt: 5000000], @"Total Delinquent",[NSNumber numberWithInt: 7000000], @"Total Open Receivables",[NSNumber numberWithInt: 49], @"DSO",@" ", @"DSO Trend",[NSNumber numberWithInt: 15], @"DSO Change",[NSNumber numberWithInt: 15], @"Percent 91+ Delinquent",[NSNumber numberWithInt: 34], @"WADD",[NSNumber numberWithInt: 49], @"Best Possible DSO",[NSNumber numberWithInt: 49], @"True DSO",[NSNumber numberWithInt: 49], @"Percent Current",[NSNumber numberWithInt: 49], @"Sales Weighted DSO",[NSNumber numberWithInt: 49], @"Weighted Average Terms",[NSNumber numberWithInt: 49], @"Average Days Delinquent",[NSNumber numberWithInt: 49], @"Weighted Average Days Late",[NSNumber numberWithInt: 49], @"Weighted Average Days Paid",[NSNumber numberWithInt: 54], @"Score",@" ", @"Score Trend",@"Hard Call", @"Assigned Strategy", @"Matt Cooke", @"Collectr",nil],nil]
*/
@interface AssignedCustomersViewController ()
@property (strong, nonatomic) IBOutlet UIButton *notesButton;
@property (strong, nonatomic) IBOutlet UIButton *detailsButton;
@property (strong, nonatomic) IBOutlet UIView *buttonsView;

@end

@implementation AssignedCustomersViewController
@synthesize notesButton = _notesButton;
@synthesize detailsButton = _detailsButton;
@synthesize buttonsView = _buttonsView;
@synthesize spreadView = _spreadView; 
@synthesize collectorName = _collectorName;

- (void) setNotesButton:(UIButton *)notesButton
{
    _notesButton = notesButton;
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    
    gradientLayer.frame = _notesButton.layer.bounds;
    [_notesButton.layer insertSublayer:gradientLayer atIndex:0];
    _notesButton.titleLabel.textColor = [UIColor whiteColor];
}

- (void) setDetailsButton:(UIButton *)detailsButton
{
    _detailsButton = detailsButton;
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    
    gradientLayer.frame = _detailsButton.layer.bounds;
    [_detailsButton.layer insertSublayer:gradientLayer atIndex:0];
    _detailsButton.titleLabel.textColor = [UIColor whiteColor];
}

- (void) setButtonsView:(UIView *)buttonsView
{
    _buttonsView = buttonsView;
    
    CAGradientLayer *gradientLayer = [ColorUtility toolBarGradientLayer];
    
    gradientLayer.frame = _buttonsView.layer.bounds;
    [_buttonsView.layer insertSublayer:gradientLayer atIndex:0];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.spreadView reloadData];
}

- (void)viewDidUnload
{
    [self setNotesButton:nil];
    [self setDetailsButton:nil];
    [self setSpreadView:nil];
    [self setButtonsView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"delinquentToCollectorSegue"])
    {
        CustomerDetailTableViewController *collectorVC = (CustomerDetailTableViewController *)segue.destinationViewController;
        collectorVC.objectType = @"Collector";
    } 
}

#pragma mark - Spread View Datasource

//no of columns per column section
- (NSInteger)spreadView:(MDSpreadView *)aSpreadView numberOfColumnsInSection:(NSInteger)section
{
    //Exclude Collector column for Collector tab.
    return [delinquentCustomerHeaders count]-1;
}

//no or rows per row section
- (NSInteger)spreadView:(MDSpreadView *)aSpreadView numberOfRowsInSection:(NSInteger)section
{
    //return [topDelinquentCustomers count];
    return [delinquentCustomerValues count];
    //return 6;
}

//no of column sections
- (NSInteger)numberOfColumnSectionsInSpreadView:(MDSpreadView *)aSpreadView
{
    //return 4;
    return 1;
}

// no of row sections
- (NSInteger)numberOfRowSectionsInSpreadView:(MDSpreadView *)aSpreadView
{
    //return 10;
    return 1;
}

#pragma mark Heights
// Comment these out to use normal values (see MDSpreadView.h)
/*
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 return 25+indexPath.row;
 }
 
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView heightForRowHeaderInSection:(NSInteger)rowSection
 {
 //    if (rowSection == 2) return 0; // uncomment to hide this header!
 
 return 22+rowSection*10;
 }
 */ 

- (CGFloat)spreadView:(MDSpreadView *)aSpreadView widthForColumnAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.row) {
        case 0:  //customer
            return 160;
            break;
        case 1:  //Total Delinquent
            return 130;
            break;
        case 2:  //Open Receivables
            return 170;
            break;
        case 3:  //DSO
            return 80;
            break;
        case 4:
            return 130;
            break;
        case 5:
            return 170;
            break;
        case 6:
            return 80;
            break;
        case 7:
            return 150;
            break;

        default:
            return 180;
            break;
    }
     
    //return 80;
}

/* 
 - (CGFloat)spreadView:(MDSpreadView *)aSpreadView widthForColumnHeaderInSection:(NSInteger)columnSection
 {
 //    if (columnSection == 2) return 0; // uncomment to hide this header!
 
 return 110+columnSection*50;
 }
 */
#pragma Cells
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForRowAtIndexPath:(NSIndexPath *)rowPath forColumnAtIndexPath:(NSIndexPath *)columnPath
{
    static NSString *cellIdentifier = @"Cell";
    
    MDSpreadViewCell *cell = [aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewCell alloc] initWithStyle:MDSpreadViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell.textLabel.text = [NSString stringWithFormat:@"Test Row %d-%d (%d-%d)", rowPath.section+1, rowPath.row+1, columnPath.section+1, columnPath.row+1];
    // cell.textLabel.text = [trxnRecordArray objectAtIndex:columnPath.row];
/*    
    switch (columnPath.row) {
            
        case 0:  //transaction
            trxnNumber = [[trxnRecordArray objectAtIndex:columnPath.row] intValue] +(arc4random()%10) + 10 * rowPath.column;
            cell.textLabel.text = [NSString stringWithFormat:@"%d", trxnNumber];
            cell.textLabel.textAlignment = UITextAlignmentLeft;
            break; 
            
        case 1:  //original amount
            originalAmount = [[trxnRecordArray objectAtIndex:columnPath.row] intValue] +(arc4random()%10000000);
            cell.textLabel.text = [currencyFormatter stringFromNumber:[NSNumber numberWithInt:originalAmount]];
            cell.textLabel.textAlignment = UITextAlignmentRight;
            break;
        case 2:  //remaining amount
            remainingAmount = (arc4random()%1000000);
            cell.textLabel.text = [currencyFormatter stringFromNumber:[NSNumber numberWithInt:remainingAmount]];
            cell.textLabel.textAlignment = UITextAlignmentRight;
            break;
        case 3:  //days late
            daysLate = (arc4random()%150);
            cell.textLabel.text = [NSString stringWithFormat:@"%d", daysLate];
            cell.textLabel.textAlignment = UITextAlignmentRight;
            break;
        default:
            cell.textLabel.text = [trxnRecordArray objectAtIndex:columnPath.row];
            cell.textLabel.textAlignment = UITextAlignmentLeft;
            break;
    }
 */
 /*   
    if (columnPath.row == 0) {
        cell.textLabel.text = [topDelinquentCustomers objectAtIndex:rowPath.row];
    } else {
        cell.textLabel.text = [detailValueList objectAtIndex:columnPath.row-1];
    }
  */
    
    NSArray *currentCustomer = [delinquentCustomerValues objectAtIndex:rowPath.row];

    cell.textLabel.text = [currentCustomer objectAtIndex:columnPath.row];        
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    if (columnPath.row == 0 || columnPath.row == 18 || columnPath.row == 19) {
        cell.textLabel.textAlignment = UITextAlignmentLeft;
    } else {
        cell.textLabel.textAlignment = UITextAlignmentRight;
    }
    return cell;
}


//There are 3 types of header cells and the following 3 methods creating these cells for each type.
//CornerHeaderCell, RowHeaderCell,ColumnHeaderCell.
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInRowSection:(NSInteger)rowSection forColumnSection:(NSInteger)columnSection
{
    static NSString *cellIdentifier = @"CornerHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleCorner reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"Cor %d-%d", columnSection+1, rowSection+1];
    
    return cell;
}
//this is actually the header for each column
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInRowSection:(NSInteger)section forColumnAtIndexPath:(NSIndexPath *)columnPath
{
    static NSString *cellIdentifier = @"RowHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleRow reuseIdentifier:cellIdentifier];
    }
    
    //cell.textLabel.text = [NSString stringWithFormat:@"Row Header %d (%d-%d)", section+1, columnPath.section+1, columnPath.row+1];
/*
    if (columnPath.row == 0) {
        cell.textLabel.text = @"Customer Name";
    } else {
        cell.textLabel.text = [detailTitleList objectAtIndex:columnPath.row-1];
    }
*/    
    cell.textLabel.text = [delinquentCustomerHeaders objectAtIndex:columnPath.row];    

    cell.textLabel.frame = cell.frame;
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    if (columnPath.row == 0 || columnPath.row == 18 || columnPath.row == 19) {
        cell.textLabel.textAlignment = UITextAlignmentLeft;
    } else {
        cell.textLabel.textAlignment = UITextAlignmentRight;
    }

    return cell;
}

//this is actually the header for each row
- (MDSpreadViewCell *)spreadView:(MDSpreadView *)aSpreadView cellForHeaderInColumnSection:(NSInteger)section forRowAtIndexPath:(NSIndexPath *)rowPath
{
    static NSString *cellIdentifier = @"ColumnHeaderCell";
    
    MDSpreadViewHeaderCell *cell = (MDSpreadViewHeaderCell *)[aSpreadView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MDSpreadViewHeaderCell alloc] initWithStyle:MDSpreadViewHeaderCellStyleColumn reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%d (%d-%d)", section+1, rowPath.section+1, rowPath.row+1];
    
    return cell;
}

@end
